package com.application.sistercityapplication.listadapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Picture;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.sistercityapplication.dto.GalleryDto;
import com.application.sistercityapplication.dto.PictureDto;
import com.application.sistercityapplication.main.GalleryPageFragment;
import com.application.sistercityapplication.main.NewDashBoardPageActivity;
import com.application.sistercityapplication.main.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.squareup.picasso.Picasso;

@SuppressLint("NewApi") public class GridViewCateImageAdapter extends BaseAdapter {

	private Activity _activity;
	private int imageWidth;
	private ViewHolder viewHolder;
	private int _cid;
	private String countryTitle;
	private SlidingMenu _sm;
	private GalleryDto galleryDto;
	private List<GalleryDto> gallerys;
	public GridViewCateImageAdapter(Activity activity,List<GalleryDto> gallerys,int imageWidth,int c_id,String countryTitle,SlidingMenu sm) {
		this._activity = activity;
		this.imageWidth = imageWidth;
		this.gallerys = gallerys;
		this._cid = c_id;
		this.countryTitle = countryTitle;
		this._sm = sm;
	}

	@Override
	public int getCount() {
		return gallerys.size();
	}

	@Override
	public Object getItem(int position) {
		return gallerys.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
//		int menu = _gridSize.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.each_box_gallery, null);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.showCase);
			viewHolder.relative = (RelativeLayout) convertView.findViewById(R.id.clickLayout);
			viewHolder.showCase.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth,imageWidth));
			viewHolder.catTitle = (TextView) convertView.findViewById(R.id.catTitle);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		galleryDto = gallerys.get(position);
		if(galleryDto.getPictures()!=null && galleryDto.getPictures().size()>0){
		Picasso.with(_activity).load(galleryDto.getPictures().get(0).getThumbnail()).into(viewHolder.showCase);
		}
		viewHolder.catTitle.setText(galleryDto.getCatTitle());
//		switch (menu) {
//		case 0:
//			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_thailand));
//			break;
//		case 1:
//			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_brunai));
//			break;
//		case 2:
//			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_singapore));
//			break;
//		case 3:
//			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_philipine));
//			break;
//		case 4:
//			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_myanmar));
//			break;
//		default:
//			break;
//		}
		
		viewHolder.relative.setOnClickListener(new GridOnclickListener(galleryDto.getPictures()));
		
		return convertView;
	}

	public class ViewHolder {
		private ImageView showCase;
		private RelativeLayout relative;
		private TextView catTitle;
	}
	public class GridOnclickListener implements OnClickListener{
		private List<PictureDto> pictures;
		public GridOnclickListener(List<PictureDto> pictures){
			this.pictures = pictures;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Fragment newContent = new GalleryPageFragment(_cid,countryTitle,_sm,pictures);
			switchFragment(newContent);
		}
		
	}
	private void switchFragment(Fragment fragment) {
		if (_activity == null)
			return;

		if (_activity instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) _activity;
			fca.switchContent(fragment);
		}
	}
}
